package com.rongxue.controller.payment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rongxue.mapper.SBankTypeMapper;
import com.rongxue.mapper.SUserBankMapper;
import com.rongxue.mapper.SUserMapper;
import com.rongxue.pojo.*;
import com.rongxue.service.BankBusinessService;
import com.rongxue.service.Impl.BankBusinessServiceImpl;
import com.rongxue.service.Impl.CashInAndOutServiceImpl;
import com.rongxue.service.shanbei.services.AccountService;
import com.rongxue.service.shanbei.util.ConfigUtil;
import com.rongxue.service.shanbei.util.Main;
import com.rongxue.service.shanbei.util.SignUtil;
import com.rongxue.service.utils.P2PMessageUtils;
import com.rongxue.utils.BankCardUtils;
import com.rongxue.utils.CommonUtils;
import com.rongxue.utils.IDCardUtils;
import com.rongxue.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;


@Controller
public class UserBankCardController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserBankCardController.class);

    @Autowired
    private BankBusinessService bankBusinessService;

    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @Autowired
    private SUserBankMapper sUserBankMapper;

    @Autowired
    private SBankTypeMapper sBankTypeMapper;
    @Autowired
    private SUserMapper sUserMapper;


    /**
     * 开户
     * @param xmlBean
     * @return
     */
    @RequestMapping("/tender/binding_bank_no")
    @ResponseBody
    public ObjectResult binding_bank_no(XMLBean xmlBean) {
        if (xmlBean.getToken()==null ||"".equals(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        String userId =CommonUtils.decToken(xmlBean.getToken());

        SUser resultUser =sUserMapper.selectUserInfoById(Long.parseLong(userId));

        if (resultUser == null){
            return ObjectResult.build("0", "用户信息错误,请重试");
        }

        if (resultUser.getCardNoAuth() == 1 && resultUser.getRealNameAuth()==1){
            return ObjectResult.build("0", "您已开户");
        }

        SUserBank sUserBank = sUserBankMapper.selectUserBankByUserId(userId);
        if (sUserBank != null){
            return ObjectResult.build("0", "您已绑定银行卡");
        }


        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "create_account_p");
        //账户类型(普通户200201,企业户200204)
        if (resultUser.getType()==2 ||resultUser.getType()==3) {
            reqMap.put("account_type", "200204");
        }
        else {
            reqMap.put("account_type", "200201");
        }
        reqMap.put("role_type", resultUser.getType().toString());//1：出借角色，2：借款角色，3：代偿角色
        P2PMessageUtils p2PMessageUtils = new P2PMessageUtils();
        String rechargeNo = p2PMessageUtils.getRechargeNo("K");
        reqMap.put("out_serial_no", rechargeNo);
        reqMap.put("callback_url", CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"tender/binding_bank_noNotify");
        reqMap.put("success_url",  CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"index/resultHtml?type=1&title=5");
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("fail_url",  CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"index/resultHtml?type=0&title=6");
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("risk_level_flag", "1");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        System.out.println(str.toString());
        JSONObject jsonObject = JSONObject.parseObject(str);

        SUser sUser = new SUser();
        sUser.setId(Long.parseLong(userId));
        sUser.setOut_serial_no(rechargeNo);
        sUser.setSequence_id(jsonObject.getString("sequence_id"));
        sUser.setOrder_id(jsonObject.getString("order_id"));
        if (resultUser.getType()==2){
            sUser.setStatus(2);
        }
        sUserMapper.updateByPrimaryKeySelective(sUser);

        if(!jsonObject.getString("code").equals("RD000000")){
            return  ObjectResult.build("0",jsonObject.getString("msg"));
        }

        return ObjectResult.build("1","成功",jsonObject.getString("url"));
    }


    /**
     * 开户回调
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/binding_bank_noNotify")
    public void binding_bank_noNotify() throws Exception {
        Map<String, Object> map = P2PMessageUtils.sign(request, "开户回调报文：");
        if (!(Boolean)map.get("flag"))return;

        ObjectResult ObjectResult = bankBusinessService.bindingBankNo((JSONObject) map.get("jsonObject"));
        if (ObjectResult.getCode().equals("0")) {
            LOGGER.info(ObjectResult.toString());
        }

        //返回回成功
        PrintWriter pw = null;
        try {
            pw = response.getWriter();
            pw.write("success");
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    /**
     * 开户结果查询
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/createAccountSrQuery")
    @ResponseBody
    public ObjectResult createAccountSrQuery(XMLBean xmlBean) {
        String userId =xmlBean.getId();
        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "create_account_sr_query");
        reqMap.put("order_id", sUser.getOrder_id());
        reqMap.put("account_type","200201");
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }


    /**
     * 网关自定义下单查询
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/gatewayQuery")
    @ResponseBody
    public ObjectResult gatewayQuery(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "gateway_query");
        reqMap.put("order_id", xmlBean.getOrder_id());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }

    /**
     * 页面接口回调重发
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/batchPageNotify")
    @ResponseBody
    public ObjectResult batchPageNotify(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "batch_page_notify_b");
        reqMap.put("req_sequence_id", xmlBean.getOrder_id());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }

    /**
     * 再贷标的余额查询
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/asset_balance_query")
    @ResponseBody
    public ObjectResult batch_notify_resend_b(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "asset_balance_query");
        reqMap.put("card_no", xmlBean.getOrder_id());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }


    /**
     * 按手机号查询电子账号信息
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/find_account_by_mobile")
    @ResponseBody
    public ObjectResult find_account_by_mobile(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "find_account_by_mobile");
        reqMap.put("mobile",xmlBean.getMobile());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }

    /**
     * 电子账户绑卡关系查询
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/tender/binding_list")
    @ResponseBody
    public ObjectResult binding_list(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "binding_list");
        reqMap.put("card_no",xmlBean.getCard_no());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }


    /**
     * 网关绑卡结果查询
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/tender/bindBankCardQuery")
    @ResponseBody
    public ObjectResult bindBankCardQuery(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "bind_bank_card_query");
        reqMap.put("card_type","0");
        reqMap.put("order_id", xmlBean.getOrder_id());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }

    /**
     * 平台营销账户基本信息查询
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/tender/marketing_query")
    @ResponseBody
    public ObjectResult marketing_query(XMLBean xmlBean) {
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "marketing_query");
        reqMap.put("type",xmlBean.getType());
        if (xmlBean.getType()==1){
            reqMap.put("card_no", "8666882121000529094");//手续费账户
        }if (xmlBean.getType()==2){
            reqMap.put("card_no", "8666882002000529088");//红包账户
        }
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if (jsonObject.getString("code").equals("RD000000")){
            return ObjectResult.build("1","",jsonObject);
        }else {
            return ObjectResult.build("0",jsonObject.getString("msg"));
        }
    }

    /**
     * 解绑银行卡
     * @param xmlBean
     * @return
     */
    @RequestMapping("/tender/relieve_bank_no")
    @ResponseBody
    public ObjectResult relieve_bank_no(XMLBean xmlBean) {
        String userId =CommonUtils.decToken(xmlBean.getToken());

        SUser resultUser =sUserMapper.selectUserInfoById(Long.parseLong(userId));

        if (resultUser == null){
            return ObjectResult.build("0", "用户信息错误,请重试");
        }
        SUserBank sUserBank = sUserBankMapper.selectUserBankByUserId(userId);
        if (sUserBank == null){
            return ObjectResult.build("0", "您未绑定银行卡");
        }

        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "unbind_bank_card");
        reqMap.put("card_no", resultUser.getCard_no2());
        reqMap.put("bank_card_no",sUserBank.getBankCardNo());
        reqMap.put("customer_no",resultUser.getCustomer_no());
        reqMap.put("serial_no", resultUser.getSerial_no());
        reqMap.put("card_type", "0");
        reqMap.put("bank_mobile",sUserBank.getMobile());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        if(!jsonObject.getString("code").equals("RD000000")){
            return  ObjectResult.build("0",jsonObject.getString("msg"));
        }
        sUserBank.setIsDeleted(1);
        sUserBankMapper.updateByPrimaryKeySelective(sUserBank);

        return ObjectResult.build("1","解绑银行卡成功");
    }

    /**
     * 绑定银行卡
     * @param xmlBean
     * @return
     */
    @RequestMapping("/tender/bindingUserBankCard")
    @ResponseBody
    public ObjectResult bindingUserBankCard(XMLBean xmlBean) {
        String userId =CommonUtils.decToken(xmlBean.getToken());

        SUser resultUser =sUserMapper.selectUserInfoById(Long.parseLong(userId));

        if (resultUser == null){
            return ObjectResult.build("0", "用户信息错误,请重试");
        }
        SUserBank sUserBank = sUserBankMapper.selectUserBankByUserId(userId);
        if (sUserBank != null){
            return ObjectResult.build("0", "您已绑定银行卡");
        }

        P2PMessageUtils p2PMessageUtils = new P2PMessageUtils();
        String rechargeNo = p2PMessageUtils.getRechargeNo("BI");

        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "bind_bank_card_p");
        reqMap.put("out_serial_no", rechargeNo);
        reqMap.put("card_no",resultUser.getCard_no2());
        reqMap.put("card_type", "0");
        reqMap.put("callback_url",CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"tender/binding_bank_noNotify");
        reqMap.put("success_url", CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"index/resultHtml?type=1&title=11");
        reqMap.put("fail_url", CashInAndOutServiceImpl.PAGE_NOTIFY_URL+"index/resultHtml?type=0&title=12");
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);


        SUser sUser = new SUser();
        sUser.setId(Long.parseLong(userId));
        sUser.setOut_serial_no(rechargeNo);
        sUser.setSequence_id(jsonObject.getString("sequence_id"));
        sUser.setOrder_id(jsonObject.getString("order_id"));;
        sUserMapper.updateByPrimaryKeySelective(sUser);

        if(!jsonObject.getString("code").equals("RD000000")){
            return  ObjectResult.build("0",jsonObject.getString("msg"));
        }

        return ObjectResult.build("1","成功",jsonObject.getString("url"));
    }


    /**
     * 获取个人银行卡列表
     *
     * @param xmlBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tender/account_info")
    public ObjectResult selectUserBank(XMLBean xmlBean) {
        if (xmlBean.getToken() == null || xmlBean.getToken().equals("")) return ObjectResult.build("0", "token不存在");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUserBank banks = sUserBankMapper.selectUserBankByListUserId(userId);

        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));

        SBankType sBankType = sBankTypeMapper.selectByBankFullName("%" + banks.getBankName().substring(0, banks.getBankName().lastIndexOf("银行") + 2) + "%");
        String str = null;
        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("service", "account_balance");
        reqMap.put("card_no", sUser.getCard_no2());
        reqMap.put("customer_no",sUser.getCustomer_no());
        reqMap.put("timestamp", String.valueOf(new Date().getTime() / 1000));
        reqMap.put("uuid", "" + System.currentTimeMillis() / 100);
        reqMap.put("sign_type", "");
        reqMap.put("encode", "UTF-8");
        reqMap.put("version", "2.0.0");
        reqMap.put("custom", "");
        reqMap.put("client", "000001");
        reqMap.put("client_ip", "47.98.107.188");
        reqMap.put("client_service", "00:16:3e:11:b0:02");
        String rft_key = ConfigUtil.getPro("bank.key");
        String rft_secret = ConfigUtil.getPro("bank.secret");
        str = AccountService.createAccount(reqMap, rft_key, rft_secret);
        JSONObject jsonObject = JSONObject.parseObject(str);

        banks.setBankCode(sBankType.getBank_code());
        banks.setMin_money("100");
        banks.setBalance(new BigDecimal(jsonObject.getString("balance")));//账户余额
        BigDecimal withdraw_balance = new BigDecimal(jsonObject.getString("withdraw_balance"));//可提现余额
        //可提现余额小于手续费返回0,大于手续费则减去手续费
        if (withdraw_balance.compareTo(CashInAndOutServiceImpl.FEE)<0){
            banks.setWithdraw_balance(new BigDecimal(0));
        }else {
            banks.setWithdraw_balance(withdraw_balance.subtract(CashInAndOutServiceImpl.FEE));
        }
        banks.setExplain("平台提现通道次日到账，收取手续费"+CashInAndOutServiceImpl.FEE+"元,如有疑问请致电客服咨询，或者咨询在线客服。");
        banks.setAccountAble(banks.getWithdraw_balance());

        //是否添加开户省和市
        if (banks.getBankCity()==null || "".equals(banks.getBankCity()) ||
                banks.getBankProvince() == null || "".equals(banks.getBankProvince())){
            banks.setIs_city(0);
        }else {
            banks.setIs_city(1);
        }

        return ObjectResult.build("1", "成功", banks);
    }

    /**
     * 获取本系统支持的银行列表
     *
     * @param xmlBean
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/public/bank_list")
    public ObjectResult selectBankList(XMLBean xmlBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        SBankType sBankType = new SBankType();
        List<SBankType> banks = sBankTypeMapper.selectBankList(sBankType);
        map.put("list", banks);
        return ObjectResult.build("1", "成功", map);
    }

    /**
     * 是否实名认证和绑卡
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/tender/realName")
    @ResponseBody
    public ObjectResult realName(XMLBean xmlBean){
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        SUser sUser = sUserMapper.selectUserByMap(map);
        map.clear();
        map.put("card_no_auth",sUser.getRealNameAuth());
        SUserBank sUserBank = sUserBankMapper.selectUserBankByUserId(userId);
        if(sUserBank==null){
            map.put("is_bank",0);
        }
        else if(sUserBank.getBankCardNo()==null ||"".equals(sUserBank.getBankCardNo())){
            map.put("is_bank",0);
        }else {
            map.put("is_bank",1);
        }

        map.put("is_sign",sUser.getIs_sign());
        map.put("type",sUser.getType());
        return ObjectResult.build("1","成功",map);
    }


    /**
     * 添加开户行省市
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/tender/provinceAndCity")
    @ResponseBody
    public ObjectResult provinceAndCity(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())||StringUtil.isEmpty(xmlBean.getBankCity())||StringUtil.isEmpty(xmlBean.getBankProvince())) {
            return ObjectResult.build("0","参谋丢失,请重试");
        }
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUserBank sUserBank = sUserBankMapper.selectUserBankByUserId(userId);
        if (sUserBank==null) return ObjectResult.build("0","未绑定银行卡");
        try {
            sUserBank.setBankProvince(xmlBean.getBankProvince());
            sUserBank.setBankCity(xmlBean.getBankCity());
            sUserBank.setModifyTime(new Date());
            sUserBankMapper.updateByPrimaryKeySelective(sUserBank);
        }catch (RuntimeException e){
            return ObjectResult.build("0","添加开户行省市失败");
        }

        return ObjectResult.build("1","添加开户行省市成功");
    }

}





